package main

import (
	"errors"
	"fmt"
)

func f1(arg int)(int,error){
	if arg == 66 {
		return -1,errors.New("发生错误啦")
	}
	return arg*2,nil
}

type argError struct {
	arg int
	prob string
}

func (e argError) Error() string  {
	return fmt.Sprint("%d - %s",e.arg,e.prob)
}

func f2(arg int) (int,error)  {
	if arg == 42 {
		return -1, argError{arg, "can't work with it"}
	}
	return arg + 3, nil
}

func TestError() {
	for _,i := range  []int{7,66}{
		if r,e:= f1(i);e != nil {
			fmt.Println("no", e)
		} else {
			fmt.Println("yes",r)
		}
	}

	for _, i := range []int{7, 66} {
		if r, e := f2(i); e != nil {
			fmt.Println("f2 failed:", e)
		} else {
			fmt.Println("f2 worked:", r)
		}
	}

	_,e:=f2(42)
	if ae,ok := e.(argError); ok {
		fmt.Println(ae.arg)
		fmt.Println(ae.prob)
	}
}
